package net.oschina.app.fragment.Explorer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import net.oschina.app.R;
import net.oschina.app.adapter.Explorer.ExplorerRecentAdapter;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.bean.ExploreEventBean;
import net.oschina.app.bean.MoveDetailBean;
import net.oschina.app.protocol.ExplorerRecentProtocol;
import net.oschina.app.protocol.me_messager.DetailExploreRecentProtocol;
import net.oschina.app.ui.DetailActivity;
import net.oschina.app.util.CommonUtils;

import java.util.List;

/**
 * Created by wei03 on 2017/1/13.
 */
public class ExplorerRecentFragment extends BaseFragment {

    private ExplorerRecentAdapter mExplorerRecentAdapter;
    private List<ExploreEventBean.EventBean> eventBean;
    @Override
    protected void OnSubItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
        final Intent intent = new Intent(getContext(), DetailActivity.class);
        final int id = eventBean.get(position).getId();
        new Thread(new Runnable() {
            @Override
            public void run() {
                DetailExploreRecentProtocol detailExploreRecentProtocol = new DetailExploreRecentProtocol(id);
                MoveDetailBean moveDetailBean = detailExploreRecentProtocol.getData(0);
                intent.putExtra("url",moveDetailBean.getPost().getUrl());
                intent.putExtra("title", getFragmentTitle());
                startActivity(intent);
            }
        }).start();
    }

    @Override
    protected BaseAdapter getRvAdapter() {
        mExplorerRecentAdapter = new ExplorerRecentAdapter();
        return mExplorerRecentAdapter;
    }

    @Override
    protected int onLoadSubData() {
        Bundle arguments = getArguments();
        String uid = "";
        if (arguments != null) {
            uid = arguments.getString("uid");
        }
        ExplorerRecentProtocol explorerRecentProtocol = new ExplorerRecentProtocol(uid);
        final ExploreEventBean exploreEventBean = explorerRecentProtocol.getData(mList.size());

        CommonUtils.runOnMainThread(new Runnable() {
            @Override
            public void run() {
//                mList.add(moveBean.getEvents());
                if (mExplorerRecentAdapter!=null){
                    eventBean = exploreEventBean.getEvents();
                    mExplorerRecentAdapter.setDatas(exploreEventBean.getEvents());
                }
            }
        });
        return STATE_SUCCESS;
    }

    @Override
    protected String getFragmentTitle() {
        return getString(R.string.actionbar_title_event_detail);
    }
}
